#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 2e5 + 10;
int p[N], d[N];
int n;
int mi = 1e9;
int find(int x)
{
	if (x != p[x]) {
		int root = find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}
void fun(int a, int b)
{
	int a1 = find(a);
	int b1 = find(b);
	if (a1 != b1) {
		p[a1] = b1;
		d[a] = d[b] + 1;
	}
	else {
		mi = min(mi, d[a] + d[b] + 1);
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)p[i] = i;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		fun(i, x);
	}
	cout << mi << endl;
	return 0;
}